/**
 * Trips Traps Trull algoritm
 */
var userCells;
var programCells;

var winningCombinations = [ [ 1, 2, 3, 4 ], [ 5, 6, 7, 8 ], [ 9, 10, 11, 12 ],
		[ 13, 14, 15, 16 ], [ 1, 5, 9, 13 ], [ 2, 6, 10, 14 ],
		[ 3, 7, 11, 15 ], [ 4, 8, 12, 16 ], [ 1, 6, 11, 16 ], [ 4, 7, 10, 13 ] ];

function getProgramCell() {
	var takeCell = playerCloseToWinning(programCells);
	if(takeCell == 0){
		takeCell = playerCloseToWinning(userCells);
		if(takeCell == 0){
			takeCell = findBestCell(); 
		}
	}	
	return takeCell;
}

function findBestCell(){
	var cellToTake = 0;
	var availableCombinations = new Array;
	if(programCells.length == 0){
		cellToTake = findFreeCorner();
	} else {
		for ( var i = 0; i < winningCombinations.length; i++) {
			var combination = winningCombinations[i];
			for ( var j = 0; j < combination.length; j++) {
				if($.inArray(combination[j], programCells) > -1){
					if(isCombinationAvailable(combination)){
						availableCombinations.push(combination);
						break;
					}
				}
			}
		}
		cellToTake = findBestCombinationCell(availableCombinations);
	}
	if(cellToTake == 0){
		cellToTake = findAvailableCombination();
		if(cellToTake == 0){
			cellToTake = getRandomAvailableCell();
		}
	}
	return cellToTake;
}

function findBestCombinationCell(combinations){
	var bestCombination = new Array();
	var maxCellsMatched = 0;
	var cellsMatched;
	var combination;
	for ( var i = 0; i < combinations.length; i++) {
		cellsMatched = 0;
		combination = combinations[i];
		for ( var j = 0; j < combination.length; j++) {
			if($.inArray(combination[j], programCells) > -1){
				cellsMatched++;
			}
		}
		if(cellsMatched >= maxCellsMatched){
			maxCellsMatched = cellsMatched;
			bestCombination = combination;
		}
	}
	var cellToTake = findOpenCell(bestCombination, programCells);
	return cellToTake;
}


function isCombinationAvailable(combination){
	var isAvailable = true;
	for ( var i = 0; i < combination.length; i++) {
		if($.inArray(combination[i], userCells) > -1){
			isAvailable = false;
			break;
		}
	}
	return isAvailable;
}

function findAvailableCombination(){
	var cellToTake = 0;
	var cellsMatched;
	var combination;
	for ( var i = 0; i < winningCombinations.length; i++) {
		cellsMatched = 0;
		combination = winningCombinations[i];
		for ( var j = 0; j < combination.length; j++) {
			if($.inArray(combination[j], getAvailableCells()) > -1){
				cellsMatched++;
			} else {
				break;
			}
		}
		if(cellsMatched == 4){
			cellToTake = combination[0];
			break;
		}
	}
	return cellToTake;
}

function findFreeCorner(){
	var corners = [1, 4, 13, 16];
	for ( var i = 0; i < corners.length; i++) {
		if($.inArray(corners[i], getAvailableCells()) < 0){
			corners.splice(i, 1);
		}
	}
	return corners[Math.floor(Math.random()*corners.length)];
}

function playerCloseToWinning(cells){
	var cellsMatched;
	var cellToTake = 0;
	if(cells.length >= 3){
		for ( var i = 0; i < winningCombinations.length; i++) {
			cellsMatched = 0;
			var combination = winningCombinations[i];
			for ( var j = 0; j < combination.length; j++) {
				if ($.inArray(combination[j], cells) > -1) {
					cellsMatched++;
				}
			}
			if(cellsMatched == 3){
				cellToTake = findOpenCell(combination, cells);
				if(cellToTake > 0)
					break;
			}
		}
	}
	return cellToTake;
}

function findOpenCell(combination, cells){
	var cellToTake = 0;
	for ( var i = 0; i < combination.length; i++) {
		if($.inArray(combination[i], cells) < 0 && $.inArray(combination[i], getAvailableCells()) > -1){
			cellToTake = combination[i];
			break;
		}
	}
	return cellToTake;
}

function checkWin() {
	if (getCurrentPlayer() == "program") {
		return checkCombinations(programCells);
	} else if (getCurrentPlayer() == "user") {
		return checkCombinations(userCells);
	}
}

function checkCombinations(playerCells) {
	var cellsMatched;
	var win = false;
	if(playerCells.length >= 4){
		for ( var i = 0; i < winningCombinations.length; i++) {
			cellsMatched = 0;
			for ( var j = 0; j < winningCombinations[i].length; j++) {
				if ($.inArray(winningCombinations[i][j], playerCells) > -1) {
					cellsMatched++;
				} else {
					break;
				}
			}
			if (cellsMatched == 4) {
				win = true;
				break;
			}
		}
	}
	return win;
}

function getWinningCombination(cells){
	var cellsMatched;
	var combination;
	for ( var i = 0; i < winningCombinations.length; i++) {
		cellsMatched = 0;
		combination = winningCombinations[i];
		for ( var j = 0; j < combination.length; j++) {
			if($.inArray(combination[j], cells) > -1){
				cellsMatched++;
 			} else {
 				break;
 			}
		}
		if(cellsMatched == 4){
			break;
		}
	}
	return combination;
}

function getRandomAvailableCell(){
	return getAvailableCells()[Math.floor(Math.random()*getAvailableCells().length)];
}